/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.data.hasher.IHasher;

/**
 * <strong>SHA1</strong> string hash.
 * 
 * <p>Orignal ActionScript 2.0 implementation by Mika Palmu in his 
 * AsCrypt library, available in http://www.meychi.com/
 * 
 * @author Mika Palmu
 * @author Romain Ecarnot
 */
class fever.data.hasher.SHA1 implements IHasher
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : IHasher;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns the MD5 hash algo
	 */
	public static function getProcess() : IHasher
	{
		if( !_instance ) _instance = new SHA1();
		return _instance;	
	}
	
	/**
	 * Hashes and returns passed-in {@code value} string.
	 */
	public function hash( value ) : String
	{
		return hex_sha1( value.toString() );	
	}
	
	
	//------------------------------------------------------------------//
	// Private implementation
	//------------------------------------------------------------------//
	
	/**
	 * Constructor.
	 */
	private function SHA1()
	{
		
	}
	
	private function hex_sha1(src:String):String
	{
		return binb2hex(core_sha1(str2binb(src), src.length*8));
	}
	
	private function core_sha1(x:Array, len:Number):Array
	{
		x[len >> 5] |= 0x80 << (24-len%32);
		x[((len+64 >> 9) << 4)+15] = len;
		var w:Array = new Array(80), a:Number = 1732584193;
		var b:Number = -271733879, c:Number = -1732584194;
		var d:Number = 271733878, e:Number = -1009589776;
		for (var i:Number = 0; i<x.length; i += 16) {
			var olda:Number = a, oldb:Number = b;
			var oldc:Number = c, oldd:Number = d, olde:Number = e;
			for (var j:Number = 0; j<80; j++) {
				if (j<16) w[j] = x[i+j];
				else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
				var t:Number = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
				e = d; d = c;
				c = rol(b, 30);
				b = a; a = t;
			}
			a = safe_add(a, olda);
			b = safe_add(b, oldb);
			c = safe_add(c, oldc);
			d = safe_add(d, oldd);
			e = safe_add(e, olde);
		}
		return new Array(a, b, c, d, e);
	}
	
	private function sha1_ft(t:Number, b:Number, c:Number, d:Number):Number
	{
		if (t<20) return (b & c) | ((~b) & d);
		if (t<40) return b ^ c ^ d;
		if (t<60) return (b & c) | (b & d) | (c & d);
		return b ^ c ^ d;
	}
	
	private function sha1_kt(t:Number):Number
	{
		return (t<20) ? 1518500249 : (t<40) ? 1859775393 : (t<60) ? -1894007588 : -899497514;
	}
	
	private function safe_add(x:Number, y:Number):Number
	{
		var lsw:Number = (x & 0xFFFF)+(y & 0xFFFF);
		var msw:Number = (x >> 16)+(y >> 16)+(lsw >> 16);
		return (msw << 16) | (lsw & 0xFFFF);
	}
	
	private function rol(num:Number, cnt:Number):Number
	{
		return (num << cnt) | (num >>> (32-cnt));
	}
	
	private function str2binb(str:String):Array
	{
		var bin:Array = new Array();
		var mask:Number = (1 << 8)-1;
		for (var i:Number = 0; i<str.length*8; i += 8) {
			bin[i >> 5] |= (str.charCodeAt(i/8) & mask) << (24-i%32);
		}
		return bin;
	}
	
	private function binb2hex(binarray:Array):String
	{
		var str:String = new String("");
		var tab:String = new String("0123456789abcdef");
		for (var i:Number = 0; i<binarray.length*4; i++) {
			str += tab.charAt((binarray[i >> 2] >> ((3-i%4)*8+4)) & 0xF) + tab.charAt((binarray[i >> 2] >> ((3-i%4)*8)) & 0xF);
		}
		return str;
	}

}